<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-18
 * Time: 18:07
 */

namespace App\Http\Controllers\Account;


use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\ArticleVail;
use App\Model\Account\Article;
use App\Tool\Error;
use Illuminate\Http\Request;

class ArticleController extends Controller
{

    /**
     * 文章列表
     */
    public function articleList(Request $request)
    {
        $where = [];
        //文章类型（1.公司简介    2. 新闻中心  3使用帮助）
        if ($request->input('a_type')) {
            $where[] = ['a_type', $request->input('a_type')];
        }
        //文章标题
        if ($request->input('a_title')) {
            $where[] = ['a_title', "like", "%".trim($request->input('a_title'))."%"];
        }
        $list = Article::articleList($where);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }
    /**
     * 添加文章
     */
    public function add(Request $request)
    {
        $parameter = $request->except('admintoken');
        if(!ArticleVail::add($parameter)){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Article::articleAdd($parameter);
        if ($status === -1) {
            return $this->error(Error::COMPANY_PROFILE_ALREADY);
        }
        if (!$status) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功！');
    }
    /**
     * 修改文章
     */
    public function edit(Request $request)
    {
        $parameter = $request->except('admintoken');
        if(!ArticleVail::edit($parameter)){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Article::articleEdit($parameter);
        if ($status === -1) {
            return $this->error(Error::COMPANY_PROFILE_ALREADY);
        }
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '修改成功！');
    }
    /**
     * 删除文章
     */
    public function articleDel(Request $request)
    {
        if(!$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Article::articleDel($request->input('a_id'));
        if (!$status) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功！');
    }
    /**
     * 禁用启用 updateStatus($aid, $status)
     */
    public function updateStatus(Request $request)
    {
        $parameter = $request->except('admintoken');
        if(!ArticleVail::updatestatus($parameter)){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Article::updatestatus($parameter['a_id'], $parameter['a_status']);
        if (!$status) {
            return $this->error(Error::OPERATION_ERROR);
        }
        return $this->success('ok', '操作成功！');
    }
}